package com.yeyks.controller.order;

import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.annotation.NeedToken;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.OrderInfoMattressParam;
import com.yeyks.common.result.OrderInfoMattressResult;
import com.yeyks.commonReference.service.mattress.OrderInfoMattressService;
import com.yeyks.web.AccountUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 共享床垫订单接口
 *
 * @author shuai
 * @date 2019-07-09
 */
@Slf4j
@Api(tags = "共享床垫订单接口")
@RestController
@RequestMapping("/order/mattress")
public class OrderInfoMattressController {
    @Autowired
    private OrderInfoMattressService mattressService;

    /**
     * 订单列表
     *
     * @return
     */
    @NeedToken
    @PostMapping(value = "/list")
    @ApiOperation(value = "订单列表", notes = "token通过请求头传入", response = OrderInfoMattressResult.class)
    public AjaxResponseBody<PageResult<OrderInfoMattressResult>> list(OrderInfoMattressParam param) {
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.Administrator != loginUser.getType() && AccountType.HotelManager != loginUser.getType()) {
            throw new ServiceException("您无权访问");
        }
        PageResult<OrderInfoMattressResult> pageResult = mattressService.myList(param, loginUser);
        return AjaxResponseBody.success(pageResult);
    }

    /**
     * 共享床垫退款
     *
     * @param orderNo
     * @return
     */
    @NeedToken
    @ApiOperation("共享床垫退款")
    @ApiImplicitParam(paramType = "query", name = "orderNo", value = "订单号", required = true, dataType = "string")
    @PostMapping("/refund")
    public AjaxResponseBody<Boolean> refund(@RequestParam(name = "orderNo") String orderNo) {
        if (Arguments.strIsNull(orderNo)) {
            throw new ServiceException("订单号不能为空");
        }
        AccountInfo loginUser = AccountUtils.getLoginUser();
        if (AccountType.Administrator != loginUser.getType()) {
            throw new ServiceException("您无权访问");
        }
        return AjaxResponseBody.success(mattressService.refund(orderNo, loginUser));
    }

}
